package javaservlet;

import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.leapmotion.leap.*;
import javaservlet.ControlInterface;


/**
 * Servlet implementation class servlet
 */
@WebServlet("/servlet")
public class servlet extends HttpServlet {
	Controller controller = null;
	SampleListener listener  = null;
	Queue<Integer> infoque = null;
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public servlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append(infoque.poll() + "");
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
		infoque = new LinkedList<Integer>();
		controller = new Controller();
		controller.enableGesture(Gesture.Type.TYPE_CIRCLE);
		controller.enableGesture(Gesture.Type.TYPE_SWIPE);
		listener = new SampleListener();
		controller.addListener(listener);
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		super.destroy();
		controller.removeListener(listener);
	}
	
	class SampleListener extends Listener {

	    public void onConnect(Controller controller) {
	        System.out.println("Connected");
	    }

	    
	    public void onFrame(Controller controller) {
	        Frame frame = controller.frame();
	        //GestureList gestures =  frame.gestures();
	        
	        for(Gesture gesture : frame.gestures()) {
				if(gesture.type() == Gesture.Type.TYPE_CIRCLE) {
					if(gesture.state() == Gesture.State.STATE_STOP) {
					CircleGesture circleGesture = new CircleGesture(gesture);
					//System.out.println(circleGesture.duration() + "");
					if(circleGesture.duration() > 150000 && circleGesture.duration() <1500000)
					if (circleGesture.pointable().direction().angleTo(circleGesture.normal()) <= Math.PI/4) {
						System.out.println("顺时针划"+"  运动时间:"+circleGesture.duration() + "");
						infoque.offer(ControlInterface.CLOCKWISE_CIRCLE);
						
						try {
							Thread.sleep(50);
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
					else {
						System.out.println("逆时针划"+"  运动时间:"+circleGesture.duration() + "");
						infoque.offer(ControlInterface.ANTI_CLOCK_CIRCLE);
					}
					}
				}
				else if(gesture.type() == Gesture.Type.TYPE_SWIPE) {
					if(gesture.state() == Gesture.State.STATE_STOP) {
						SwipeGesture swipegesture = new SwipeGesture(gesture);
						System.out.println("横扫  向量："+swipegesture.pointable().direction().toString());
						if(Math.abs(swipegesture.pointable().direction().getX())>0.2)
						if(swipegesture.pointable().direction().getX()>0 ) {

							System.out.println("右滑");
							infoque.offer(ControlInterface.SWIPE_RIGHT);
						}else {
							System.out.println("左滑");
							infoque.offer(ControlInterface.SWIPE_LEFT);
						}
						//
						try {
							Thread.sleep(500);
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
					
				}
			}
	        if(frame.fingers().count() == 10) {
	        	infoque.offer(ControlInterface.TEN_FINGERS);
	        	try {
	        	Thread.sleep(500);
	        	}catch(Exception e) {
	        		e.printStackTrace();
	        	}
	        }
	        /*
	        System.out.println("Frame id: " + frame.id()
	                       + ", timestamp: " + frame.timestamp()
	                       + ", hands: " + frame.hands().count()
	                       + ",angle :"+frame.hand(1).fingers().count()+ ""
	                       + ", fingers: " + frame.fingers().count());
	                       */
	    }
	    
	    
	}
}
